﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.ComControls;
using MINERP.SamllPicPrint.View;
using MINERP.MODEL.SmallPic;
using MINERP.MODEL.FoundationModel;
using MINERP.SamllPicPrint.Presenter;
using MINERP.SamllPicPrint.Forms;
using MINERP.SamllPicPrint.Properties;
using System.Configuration;
using MINERP.COM;
using System.Runtime.InteropServices;

namespace MINERP.SamllPicPrint
{
    public partial class FrmMain : FrmParent, IMainView
    {
        private int _curTechId = 0;
        private int _curUserId = 0;
        private int _printerId = 0;

        private MainPresenter _presenter;

        [DllImport("shell32.dll ")]
        public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);

        public FrmMain()
        {
            InitializeComponent();
            base.InitiAuthority();
            barStaticItem1.Caption = base.userName;

        }

        #region IMainView 成员

        public int CurTechId
        {
            get
            {
                return _curTechId;
            }
            set
            {
                _curTechId = value;
            }
        }

        public int OperUid
        {
            get
            {
                return _curUserId;
            }
            set
            {
                _curUserId = value;
            }
        }
        public bool PrintDialog { get; set; }
        public int PrinterId
        {
            get
            {
                return _printerId;
            }
            set
            {
                _printerId = value;
            }
        }
       public  PrinterConfigInfo _printerConfigInfo
        {
            get;
            set;
        }
        public void ShowTaskList(List<ProductView> _curTaskList)
        {
            this.gridControl1.DataSource = _curTaskList;
            this.gridControl1.RefreshDataSource();
            barListItem1.Caption = _curTaskList.Count.ToString();
        }

        #endregion

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //全选 
            _presenter.FullSelect();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            //反选
            _presenter.ReSelect();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            _presenter.ExPortPic();//导出照片
            gridControl1.RefreshDataSource();
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            _presenter.PrintSelectTask();//打印条码
        }

        private void simpleButton5_Click(object sender, EventArgs e)//完成选中的产品
        {
            Cursor = Cursors.WaitCursor;
            _presenter.CompleteSelectTask();
            Cursor = Cursors.Default;
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            //刷新
            this.Cursor = Cursors.WaitCursor;
            _presenter.IniFrmView();
            Cursor = Cursors.Default;
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmSelSavePicDir frm = new FrmSelSavePicDir();
            frm.PrinterId = _printerId;
            frm.ShowDialog();
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmMain.GetInstance(this.userId);
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            _curTechId = base.CurrentTechID;
            _curUserId = base.userId;
            _printerId = base.PrinterId;
            
            barStaticItem1.Caption = base.userName;
            chkMoveByPhotoNum.Checked = Settings.Default.MoveByPhotoNum;
            try
            {
                //chkMoveByPhotoNum.Checked =Convert.ToBoolean( ConfigurationManager.AppSettings.Get("MoveByPhotoNum"));
                checkBoxAutoRecover.Checked = Convert.ToBoolean(ConfigurationManager.AppSettings.Get("AutoRecoverPath"));
                checkBoxIsShowDialig.Checked = Convert.ToBoolean(ConfigurationManager.AppSettings.Get("PrintDialog"));

            }
            catch (Exception)
            {
                chkMoveByPhotoNum.Checked = true;
                throw;
            }

            DataTransfer.MoveByPhotoNum = chkMoveByPhotoNum.Checked;
            DataTransfer.AutoRecoverPath = checkBoxAutoRecover.Checked;
            _presenter = new MainPresenter(this);

        }

        private void simpleButton7_Click(object sender, EventArgs e)
        {
            //打印某产品的条码信息
            string strCode = txtProductCode.Text.Trim();
            if (string.IsNullOrEmpty(strCode))
            {
                MessageBox.Show("请输入产品编号！");
            }
            else
            {
                if (strCode.Length < 10)
                {
                    int OrderProductId = Convert.ToInt32(strCode);
                    strCode = "2" + string.Format("{0:d9}", OrderProductId);
                }

                int ret = _presenter.PrintOrderInfo(strCode);
           
            }
        }

        private void chkMoveByPhotoNum_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.MoveByPhotoNum = chkMoveByPhotoNum.Checked;
            Properties.Settings.Default.Save();
            DataTransfer.MoveByPhotoNum = chkMoveByPhotoNum.Checked;
        }

        private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle>-1)
            {
                if (((ProductView)gridView1.GetRow(e.RowHandle)).Status == 1)
                {
                    e.Appearance.BackColor = Color.GreenYellow;//已导过片
                }
            }
        }

        private void btnOpenHotFolder_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //打开定单目录       
            string infoPath = _presenter.GetPrinterInfo(_printerId).HotDirPath;
            ShellExecute(IntPtr.Zero, new StringBuilder("open"), new StringBuilder("explorer.exe"),
                                     new StringBuilder(infoPath), new StringBuilder(string.Empty), 1);
        }

        private void checkBoxAutoRecover_CheckedChanged(object sender, EventArgs e)
        {
            ModifyConfigurationOP.SetConfig("AutoRecoverPath", checkBoxAutoRecover.Checked ? "True" : "False");
            DataTransfer.AutoRecoverPath = checkBoxAutoRecover.Checked;
        }

        private void checkBoxIsShowDialig_CheckedChanged(object sender, EventArgs e)
        {
            //ModifyConfigurationOP.SetConfig("PrintDialog", checkBoxIsShowDialig.Checked ? "True" : "False");
            if (checkBoxIsShowDialig.Checked)
            {
                Properties.Settings.Default.PrintDialog = true;
                Properties.Settings.Default.Save();
            }
            else
            {
                Properties.Settings.Default.PrintDialog = false;
                Properties.Settings.Default.Save();
            }
        }

        private void checkBoxIsAssignGrid_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.IsAssignGrid = checkBoxIsAssignGrid.Checked;
            Properties.Settings.Default.Save();
        }
    }
}
